package com.share.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.share.domain.SysMenu;
import com.share.domain.SysRole;
import com.share.domain.SysUser;
import com.share.mapper.SysMenuMapper;
import com.share.mapper.SysRoleMapper;
import com.share.service.SysRoleService;
import com.share.service.SysUserService;
import com.share.mapper.SysUserMapper;
import com.share.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
    implements SysUserService{


    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    SysRoleMapper sysRoleMapper;

    @Autowired
    SysMenuMapper sysMenuMapper;

    @Autowired
    SysRoleService sysRoleService;

    public SysUser getByUsername(String username) {

        return  sysUserMapper.selectOne(new QueryWrapper<SysUser>().eq("username", username));
    }




    public String getUserAuthorityInfo(Long userId) {
        StringBuffer authority = new StringBuffer();
        //根据用户id获取所有的角色信息
        List<SysRole> roleList = sysRoleMapper.selectList(new QueryWrapper<SysRole>().inSql("id", "select role_id from sys_user_role where user_id =" + userId));
        if (roleList.size()>0){
            String roleCodeStr = roleList.stream().map(r -> "ROLE_" + r.getCode()).collect(Collectors.joining(","));
            authority.append(roleCodeStr);
        }
        Set<String> menuCodeSet = new HashSet<>();
        //遍历所有的角色，获取所有的菜单权限，而且不重复
        for (SysRole sysRole : roleList) {
            List<SysMenu> sysMenuList = sysMenuMapper.selectList(new QueryWrapper<SysMenu>().inSql("id", "select menu_id from sys_role_menu where role_id = " + sysRole.getId()));
            for (SysMenu sysMenu : sysMenuList) {
                String perms = sysMenu.getPerms();
                if (StringUtil.isNotEmpty(perms)){
                    menuCodeSet.add(perms);
                }
            }
        }
        if (menuCodeSet.size()>0){
            authority.append(",");
            String menuCodeStr = menuCodeSet.stream().collect(Collectors.joining(","));
            authority.append(menuCodeStr);
        }

        //System.out.println("authority:"+authority.toString());
        return authority.toString();
    }


    public Page<SysUser> findPage(String username, Page page) {

        Page<SysUser> userPage = this.page(page,
                new QueryWrapper<SysUser>().like(StrUtil.isNotBlank(username),"username",username));

        userPage.getRecords().stream().forEach(u->{
            u.setRoles(sysRoleService.listRolesByUserId(u.getId()));
        });
        return userPage;
    }
}




